package com.gmss.crawler.module.retraction.reason.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.CommonEnum;
import com.gmss.crawler.module.retraction.reason.domain.RPaperReason;
import com.gmss.crawler.module.retraction.reason.service.IRPaperReasonService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 撤稿原因表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/rPaperReason")
public class RPaperReasonController extends Base {

    
    @Resource
    private IRPaperReasonService iRPaperReasonService;

    /**
    * 查询列表
    */
    @PostMapping("/list")
    public String list(@RequestBody RPaperReasonReqVo rPaperReason ){
    final LambdaQueryWrapper<RPaperReason> lambda = new QueryWrapper<RPaperReason>().lambda();
        this.buildCondition(lambda,rPaperReason);
        return respJson(CommonEnum.SUCCESS, iRPaperReasonService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public String page(@RequestBody RPaperReasonReqVo pageParam) {
        final LambdaQueryWrapper<RPaperReason> lambda = new QueryWrapper<RPaperReason>().lambda();
        this.buildCondition(lambda, pageParam);
        final IPage<RPaperReason> page = iRPaperReasonService.page(new Page<>(Constants.PAGE_NO, Constants.PAGE_SIZE), lambda);
        return respJson(CommonEnum.SUCCESS, page);
    }


        /**
        * 构造查询条件
        * @param lambda
        * @param param
        */
        private void buildCondition(LambdaQueryWrapper<RPaperReason> lambda, RPaperReasonReqVo param){
            if(!StringUtils.isEmpty(param.getId())){
                lambda.eq(RPaperReason::getId, param.getId());
            }
            if(!StringUtils.isEmpty(param.getRPaperId())){
                lambda.eq(RPaperReason::getRPaperId, param.getRPaperId());
            }
            if(!StringUtils.isEmpty(param.getReason())){
                lambda.eq(RPaperReason::getReason, param.getReason());
            }
            if(!CollectionUtils.isEmpty(param.getCreateTimeList())){
                lambda.ge(RPaperReason::getCreateTime, param.getCreateTimeList().get(0));
                lambda.le(RPaperReason::getCreateTime, param.getCreateTimeList().get(1));
            }
            if(!StringUtils.isEmpty(param.getCreateUser())){
                lambda.eq(RPaperReason::getCreateUser, param.getCreateUser());
            }
            if(!CollectionUtils.isEmpty(param.getUpdateTimeList())){
                lambda.ge(RPaperReason::getUpdateTime, param.getUpdateTimeList().get(0));
                lambda.le(RPaperReason::getUpdateTime, param.getUpdateTimeList().get(1));
            }
            if(!StringUtils.isEmpty(param.getUpdateUser())){
                lambda.eq(RPaperReason::getUpdateUser, param.getUpdateUser());
            }
            lambda.orderBy(true,false, RPaperReason::getId);
        }


        /**
         * 请求model
         */
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        private static class RPaperReasonReqVo extends RPaperReason {
            private List<String> createTimeList; // 创建时间起止
            private List<String> updateTimeList; // 更新时间起止
        }


}
